package com.hwadee.ems.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.Exam;
import com.hwadee.ems.domain.vo.ExamVO;
import com.hwadee.ems.service.IExamService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 考试 前端控制器
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Slf4j
@RestController
@RequestMapping("/exam")
public class ExamController {

    @Resource
    private IExamService examService;

    @GetMapping("/page")
    public Result<IPage<ExamVO>> page(
        PageQuery pageQuery,
        @RequestParam(required = false) String courseName,
        @RequestParam(required = false) String classroomName,
        @RequestParam(required = false) String className
    ) {
        return examService.page(
            pageQuery,
            courseName,
            classroomName,
            className
        );
    }

    @PostMapping
    public Result<Long> save(@RequestBody Exam exam) {
        if (examService.save(exam)) {
            log.info("save exam success, exam: " + exam);
            return Result.ok(exam.getId());
        } else {
            log.error("save exam failed, exam: " + exam);
            return Result.error();
        }
    }

    @PutMapping
    public Result<Void> updateById(@RequestBody Exam examEntity) {
        if (examService.updateById(examEntity)) {
            log.info("update exam success, exam: " + examEntity);
            return Result.ok();
        } else {
            log.error("update exam failed, exam: " + examEntity);
            return Result.error();
        }
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        if (examService.removeById(id)) {
            log.info("remove exam success, id: " + id);
            return Result.ok();
        } else {
            log.error("remove exam failed, id: " + id);
            return Result.error();
        }
    }
    
    @GetMapping("/page_by_invigilator_id")
    public Result<IPage<ExamVO>> pageByInvigilatorId(
        PageQuery pageQuery,
        @RequestParam Long invigilatorId,
        @RequestParam(required = false) String courseName,
        @RequestParam(required = false) String classroomName,
        @RequestParam(required = false) String className
    ) {
        return examService.pageByInvigilatorId(
            pageQuery,
            invigilatorId, 
            courseName,
            classroomName,
            className
        );
    }

    @GetMapping("/page_by_student_id")
    public Result<IPage<ExamVO>> pageByStudentId(
        PageQuery pageQuery,
        @RequestParam Long studentId,
        @RequestParam(required = false) String courseName,
        @RequestParam(required = false) String classroomName,
        @RequestParam(required = false) String className
    ) {
        return examService.pageByStudentId(
            pageQuery,
            studentId,
            courseName,
            classroomName,
            className
        );
    }

}